#include "StdAfx.h"
#include "MC3E.h"


MC3E::MC3E(void)
{
	m_strHead_1 = "3530303030304646303346463030";
	m_strSize_2 = "";
	m_strHead_3 = "30303130";

	m_strWriteOrder_4 = "31343031";
	m_strReadOrder_4 = "30343031";
	m_strSubHead_5 = "30303030";
	m_strDeviceType_D_6 = "442A";
	m_strDeviceType_W_6 = "572A";

	m_strStartAddr_7 = "";
	m_strDataSize_8 = "";
	m_strData_9 = "";
}


MC3E::~MC3E(void)
{
}

char ConvertHexChar(char ch)
{
	if((ch>='0')&&(ch<='9'))   
		return   ch+0x30;   
	else   if((ch>='A')&&(ch<='F'))   
		return   ch-'A'+10;   
	else   if((ch>='a')&&(ch<='f'))   
		return   ch-'a'+10;   
	else   return   (-1);  

}

CString String2Hex(CString strData)
{
	CString strRet = "",strTmp;
	for (int i=0;i<strData.GetLength();i++)
	{
		char cData = strData.GetAt(i);
		//int iData = (int)cData;
		strTmp.Format("%X",cData);

		strRet += strTmp;
	}

	return strRet;
}

CString BuildData_Hex(int iData)
{
	CString strRet = "",strData;


	strData.Format("%4X",iData);
	strData.Replace(' ','0');

	strRet = String2Hex(strData);

	return strRet;
}


CString BuildData_Addr(int iData)
{
	CString strRet = "",strData;


	strData.Format("%6d",iData);
	strData.Replace(' ','0');
	strRet = String2Hex(strData);

	return strRet;
}

bool MC3E::CheckResult_Write(CString strRestult)
{
	if (strRestult == "D00000FF03FF0000040000")
	{
		return true;
	}

	return false;
}

CString MC3E::BuildData_Write(char cDeviceType,short *buf,int iDataLen,int iStartAddr)
{
	CString strRet = "";
	
	strRet = m_strHead_1;
	
	m_strSize_2 = BuildData_Hex(24+iDataLen*4);

	strRet += m_strSize_2;
	strRet += m_strHead_3;
	strRet += m_strWriteOrder_4;
	strRet += m_strSubHead_5;
	if (cDeviceType == 'D')
	{
		strRet += m_strDeviceType_D_6;
	} 
	else
	{
		strRet += m_strDeviceType_W_6;
	}

	m_strStartAddr_7 = BuildData_Addr(iStartAddr);
	strRet += m_strStartAddr_7;

	m_strDataSize_8 = BuildData_Hex(iDataLen);
	strRet += m_strDataSize_8;

	//
	CString strData = "";
	m_strData_9 = "";
	for (int i = 0;i<iDataLen;i++)
	{
		strData = BuildData_Hex(buf[i]);
		m_strData_9 += strData;
	}
	strRet += m_strData_9;

	return strRet;
}


CString MC3E::BuildData_Read(char cDeviceType,int iDataLen,int iStartAddr)
{
	CString strRet = "";

	strRet = m_strHead_1;

	m_strSize_2 = BuildData_Hex(24);

	strRet += m_strSize_2;
	strRet += m_strHead_3;
	strRet += m_strReadOrder_4;
	strRet += m_strSubHead_5;
	if (cDeviceType == 'D')
	{
		strRet += m_strDeviceType_D_6;
	} 
	else
	{
		strRet += m_strDeviceType_W_6;
	}

	m_strStartAddr_7 = BuildData_Addr(iStartAddr);
	strRet += m_strStartAddr_7;

	m_strDataSize_8 = BuildData_Hex(iDataLen);
	strRet += m_strDataSize_8;

	return strRet;
}

char ConvertHexChar_Send(char ch)
{
	if((ch>='0')&&(ch<='9'))   
		return   ch-0x30;   
	else   if((ch>='A')&&(ch<='F'))   
		return   ch-'A'+10;   
	else   if((ch>='a')&&(ch<='f'))   
		return   ch-'a'+10;   
	else   return   (-1);  

}
int MC3E::String2Hex_SocketSendData(CString str, CByteArray &senddata,char* tag)
{
	int   hexdata,lowhexdata;   
	int   hexdatalen=0;   
	int   len=str.GetLength();   
	senddata.SetSize(len/2);   
	for(int   i=0;i<len;)   
	{   
		char   lstr,hstr;
		hstr=str[i];
		/*
		if(!strcmp(&hstr,tag))
				{   
					i++;   
					continue;   
				}*/
		   
		i++;   
		if(i>=len)   
			break;   
		lstr=str[i];   
		hexdata=ConvertHexChar_Send(hstr);   
		lowhexdata=ConvertHexChar_Send(lstr);   
		if((hexdata==16)||(lowhexdata==16))   
			break;   
		else     
			hexdata=hexdata*16+lowhexdata;   
		i++;   
		senddata[hexdatalen]=(char)hexdata;   
		hexdatalen++;   
	}   
	senddata.SetSize(hexdatalen);   
	return   hexdatalen; 
}

